/**
 * Mscratch.import加载文件的相对路径
 * 如果使用绝对路径，则使用原生的importScript方法
 * eg: importScript('https://170.0.0.1:8502/static/extensions/demo/sensorium.js')
 */
Mscratch.import('/demo/sensorium.js');
const sensorium = new Sensorium();
const mcore = sensorium.createMcore();

function McoreExtension() {
    // 多语言转译对照表
    this.transition_map = {
        en: {
            mcar_move_with_time: '%1 at power %2 for %3 secs',
            mcar_when_press_button: 'when press button %1',
            mcar_is_press_button: 'button %1 is pressed ?',
            mcar_lighess: 'lightness',
            forward: 'forward',
            back: 'back',
            left: 'left',
            right: 'right'
        },
        zh: {
            mcar_move_with_time: '%1 动力 %2 持续 %3 秒',
            mcar_when_press_button: '当按下轻触开关 %1',
            mcar_is_press_button: '轻触开关 %1 被按下？',
            mcar_lighess: '光照强度',
            forward: '前进',
            back: '后退',
            left: '左转',
            right: '右转'
        }
    }
}

McoreExtension.prototype.getInfo = function () {
    const MAP = this.transition_map;
    // Mscratch.lang为主进程语言设置
    const MSG = MAP[Mscratch.lang];
    return {
        // 可同时为多个设备角色添加扩展块, 不设置则为所有角色添加
        // sprite: 'mEos' 单个角色扩展
        // sprites: ['mEos', 'Mcore'] 多角色扩展
        // sprite: null 或者 sprites: null 所有角色扩展
        // devices: 'codey',
        blocks: {
            demo_mcar_move_with_time: {
                initJson: {
                    // 如果不想支持多语言，可直接写字符串
                    // eg: '%1 动力 %2 持续 %3 秒'
                    message0: MSG.mcar_move_with_time,
                    args0: [
                        {
                            type: Mscratch.ArgumentType.menu,
                            name: 'DIRECTION',
                            options: [
                                [MSG.forward, 'forward'],
                                [MSG.back, 'back'],
                                [MSG.left, 'left'],
                                [MSG.right, 'right'],
                            ]
                        },
                        {
                            type: Mscratch.ArgumentType.numberMenu,
                            name: "POWER",
                            options: [
                                ['100', 100],
                                ['50', 50],
                                ['0', 0],
                                ['-50', -50],
                                ['-100', -100],
                            ],
                            value: 50
                        },
                        {
                            type: Mscratch.ArgumentType.math,
                            name: 'DURATION',
                            value: 1
                        }
                    ],
                    colours: Mscratch.Colours.motion,
                    category: 'motion',
                    extensions: [
                        Mscratch.Extensions.shape.statement
                    ]
                },
                func: function (args) {
                    let duration = Number(args.DURATION) * 1000;
                    let direction = args.DIRECTION;
                    let speed = Number(args.POWER);
                    const DIRECTION2SPEED = {
                        'forward': [-1, 1],
                        'back': [1, -1],
                        'left': [1, 1],
                        'right': [-1, -1]
                    };
                    let leftDir = DIRECTION2SPEED[direction][0];
                    let rightDir = DIRECTION2SPEED[direction][1];
                    let protocol = mcore.VirtualJoystick()
                        .leftSpeed(leftDir * speed)
                        .rightSpeed(rightDir * speed)
                        .protocol();

                    Mscratch.remote.sendProtocol(protocol, false).then(function () {
                        setTimeout(function () {
                            protocol = mcore.VirtualJoystick().stop().protocol();
                            Mscratch.remote.sendProtocol(protocol);
                        }, duration);
                    });
                },
                codes: {
                    python: {
                        imports: ['demo'],
                        code: 'mcar.%1(%2, %3)'
                    }
                }
            },
            demo_mcar_when_press_button: {
                initJson: {
                    message0: MSG.mcar_when_press_button,
                    args0: [
                        {
                            type: Mscratch.ArgumentType.fieldMenu,
                            name: 'BUTTON',
                            options: [
                                ['A', 'A'],
                                ['B', 'B'],
                                ['C', 'C'],
                            ]
                        }
                    ],
                    colours: Mscratch.Colours.events,
                    category: 'ext',
                    extensions: [
                        Mscratch.Extensions.shape.hat
                    ]
                },
                func: function (args) {
                },
                codes: {
                    python: {
                        imports: ['meos', 'demo'],
                        code: 'mcar.on_button'
                    }
                }
            },
            demo_mcar_is_press_button: {
                initJson: {
                    message0: MSG.mcar_is_press_button,
                    args0: [
                        {
                            type: Mscratch.ArgumentType.fieldMenu,
                            name: 'BUTTON',
                            options: [
                                ['A', 'A'],
                                ['B', 'B'],
                                ['C', 'C'],
                            ]
                        }
                    ],
                    colours: Mscratch.Colours.sensing,
                    category: 'ext',
                    extensions: [
                        Mscratch.Extensions.output.boolean
                    ]
                },
                func: function (args) {
                },
                codes: {
                    python: {
                        imports: ['meos', 'demo'],
                        code: 'demo.is_button(\'{0}\')'
                    }
                }
            },
            demo_mcar_lighess: {
                initJson: {
                    message0: MSG.mcar_lighess,
                    args0: [
                    ],
                    colours: Mscratch.Colours.sensing,
                    category: 'ext',
                    extensions: [
                        Mscratch.Extensions.output.number
                    ]
                },
                func: function (args) {
                },
                codes: {
                    python: {
                        imports: ['meos', 'demo'],
                        code: 'mcar.light_sensor()'
                    }
                }
            }
        }
    }
}

// 注册扩展，主进程将对扩展进行加载
Mscratch.register(new McoreExtension());
